package com.videoaggre.zcl.ui.activity.wallpaper

import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.commit
import com.lxj.xpopup.XPopup
import com.videoaggre.zcl.R
import com.videoaggre.zcl.bean.WallpaperItem
import com.videoaggre.zcl.common.SP_KEY_WALLPAPER
import com.videoaggre.zcl.common.getString
import com.videoaggre.zcl.ui.activity.BaseActivity
import com.videoaggre.zcl.ui.fragment.enterdesk.EnterdeskFragment
import com.videoaggre.zcl.ui.fragment.meizuo.MeizuoMainFragment
import com.videoaggre.zcl.ui.widget.TitleBarLayout
import com.videoaggre.zcl.utils.FragmentChangeManager
import com.videoaggre.zcl.utils.SPUtils
import kotlinx.android.synthetic.main.activity_fragment.*

class WallpaperMainActivity : BaseActivity(R.layout.activity_fragment) {
    companion object {
        fun getWallpaperItem() = mutableListOf<WallpaperItem>().apply {
            add(WallpaperItem(getString(R.string.enterdesk_text_title), EnterdeskFragment()))
            add(WallpaperItem(getString(R.string.meizuo_text_title), MeizuoMainFragment()))
        }
    }

    private val mWallpaperItems = getWallpaperItem()
    private var mFragmentChangeManager: FragmentChangeManager? = null
    override fun initView() {
        mFragmentChangeManager = FragmentChangeManager(supportFragmentManager, R.id.frameLayout, mWallpaperItems.map { it.container }.toMutableList())
        val wallpaperPosition = SPUtils.getInt(SP_KEY_WALLPAPER, 0)
        change(if (wallpaperPosition < mWallpaperItems.size) wallpaperPosition else wallpaperPosition)
        titleView.changeStyle(TitleBarLayout.TBL_LOGIN)
        titleView.rightImage = R.mipmap.icon_switch
        bindViewClick(titleView.rightImageView)
    }

    override fun onClick(v: View?) {
        when (v) {
            titleView.rightImageView -> XPopup.Builder(this)
                .isDestroyOnDismiss(true)
                .asCenterList("切换壁纸源", mWallpaperItems.map { it.name }.toTypedArray()) { position, text -> change(position) }
                .show()
        }
    }

    private fun change(position: Int) {
        SPUtils.put(SP_KEY_WALLPAPER, position)
        mFragmentChangeManager?.setFragments(position)
        titleView.titleStr = mWallpaperItems[position].name
    }
}